{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ***运维开发基础---环境准备***\n",
    "\n",
    "\n",
    "1. python开发环境入门安装\n",
    "2. 开发工具VSCODE的基本使用\n",
    "3. 如何使用jupyter进行python学习和做笔记；\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ***1.1、分享说明***\n",
    "\n",
    "写脚本就是写代码就是编程，会编程会用代码是可以极大提高我们的工作效率，加上目前有AI加持，写代码的入门门槛已经比以前低多了。\n",
    "\n",
    "python是公司其中一个应用比较广泛的编程脚本语言，相对java\\C#的难度要低很多，因此只要之前学过C语言或者shell、sql等脚本，再学python就简单很多。\n",
    "\n",
    "学习一门编程语言的关键就是  开发环境配置好 + 一个顺手易用的编码工具 ，工作用得上写几次之后，你会发现哎哟不错哦"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2、开发环境安装与设置\n",
    "\n",
    "powershell窗口执行命令，建议以管理员身份运行。\n",
    "\n",
    "**1、安装acconda**\n",
    "\n",
    "下载地址，安装一直默认就好；\n",
    "\n",
    "\n",
    "**2、配置环境变量**\n",
    "\n",
    "将以下目录添加到path环境变量\n",
    "\n",
    "C:\\ProgramData\\anaconda3\\Scripts(以个人电脑安装目录为准)\n",
    "\n",
    "powershell执行命令验证结果：conda --version \n",
    "\n",
    "**3、创建python运行环境**\n",
    "\n",
    "conda create -n agiclass python=3.9\n",
    "\n",
    "**4、 配置 PowerShell 以使用 Conda 环境**\n",
    "\n",
    "conda init powershell\n",
    "\n",
    "💡如PowerShell 的执行策略限制了脚本的运行会有如下异常\n",
    "\n",
    "<img src=\"./error1.png\" style=\"margin-left: 0px\" width=\"800px\">\n",
    "\n",
    "执行以下命令\n",
    "Set-ExecutionPolicy RemoteSigned\n",
    "\n",
    "**5、切换运行环境**\n",
    "\n",
    "conda deactivate      禁用当前生效的环境\n",
    "\n",
    "conda activate mkz_py39env   启用制定环境\n",
    "\n",
    "conda env list   查看本地环境\n",
    "\n",
    "**6、设置pip源**\n",
    "\n",
    "将 pip（Python 的包管理工具）升级到最新版本\n",
    "python -m pip install --upgrade pip\n",
    "\n",
    "设置默认镜像\n",
    "pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple\n",
    "\n",
    "具体可阅读：pypi | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **1.2、开发环境安装与设置**\n",
    "\n",
    "**1、安装VSCODE**\n",
    "下载地址，安装一直默认就好；\n",
    "\n",
    "**2、安装python编译插件**\n",
    "\n",
    "<img src=\"./python_ex_vs.png\" style=\"margin-left: 0px\" width=\"300px\">\n",
    "\n",
    "**3、 安装jupyter插件**\n",
    "\n",
    "<img src=\"./jupyter_ex_vs.png\" style=\"margin-left: 0px\" width=\"300px\">\n",
    "\n",
    "**4、安装ipykernel**\n",
    "\n",
    "能够在jupyter notebook笔记中运行python代码，自动补全、魔法命令等\n",
    "\n",
    "pip install ipykernel   \n",
    "\n",
    "**5、Hello  World**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "print(\"hello world\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**6、数据表对比处理**\n",
    "\n",
    "1）根据代码生成测试输入数据\n",
    "\n",
    "https://tongyi.aliyun.com/qianwen/?spm=5176.2810346&code=lapnbfugti&utm_content=se_1017928895&sessionId=86716aedd42e4f389488b1c0c80e1e5e\n",
    "\n",
    "2）运行测试数据生成代码\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试数据已成功生成！\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import openpyxl\n",
    "\n",
    "# 创建A.csv的数据\n",
    "data_a = {\n",
    "    'ip_a': ['192.168.0.1', '192.168.0.2', '192.168.0.3', '192.168.0.4'],\n",
    "    'info_a': ['Data1_A', 'Data2_A', 'Data3_A', 'Data4_A']\n",
    "}\n",
    "df_a = pd.DataFrame(data_a)\n",
    "df_a.to_csv('A.csv', index=False)\n",
    "\n",
    "# 创建B.csv的数据\n",
    "data_b = {\n",
    "    'ip_b': ['192.168.0.2', '192.168.0.3', '192.168.0.5', '192.168.0.6'],\n",
    "    'info_b': ['Data2_B', 'Data3_B', 'Data5_B', 'Data6_B']\n",
    "}\n",
    "df_b = pd.DataFrame(data_b)\n",
    "df_b.to_csv('B.csv', index=False)\n",
    "\n",
    "print(\"测试数据已成功生成！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "处理完成！\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 读取csv文件\n",
    "df_a = pd.read_csv('A.csv')\n",
    "df_b = pd.read_csv('B.csv')\n",
    "\n",
    "# 关联查询并保存结果\n",
    "merged_df = pd.merge(df_a, df_b, left_on='ip_a', right_on='ip_b', how='inner')\n",
    "merged_df.to_csv('a交b.csv', index=False)\n",
    "\n",
    "# 在A表中找出ip_a值不在B表中的数据行\n",
    "not_in_b = df_a[~df_a['ip_a'].isin(df_b['ip_b'])]\n",
    "\n",
    "# 在B表中找出ip_b值不在A表中的数据行\n",
    "not_in_a = df_b[~df_b['ip_b'].isin(df_a['ip_a'])]\n",
    "\n",
    "# # 创建一个Excel writer对象\n",
    "with pd.ExcelWriter('highlighted_results.xlsx', engine='openpyxl') as writer:\n",
    "    # 写入原始数据\n",
    "    df_a.to_excel(writer, sheet_name='A表', index=False)\n",
    "    df_b.to_excel(writer, sheet_name='B表', index=False)\n",
    "    \n",
    "    # 将不匹配的数据行写入对应的表格并高亮显示\n",
    "    not_in_b.to_excel(writer, sheet_name='A表_未匹配', index=False)\n",
    "    worksheet = writer.sheets['A表_未匹配']\n",
    "    for cell in worksheet[\"A1:A{}\".format(len(not_in_b) + 1)]:\n",
    "        for c in cell:\n",
    "            c.fill = openpyxl.styles.PatternFill(start_color=\"FFCCCB\", end_color=\"FFCCCB\", fill_type = \"solid\")\n",
    "    \n",
    "    not_in_a.to_excel(writer, sheet_name='B表_未匹配', index=False)\n",
    "    worksheet = writer.sheets['B表_未匹配']\n",
    "    for cell in worksheet[\"A1:A{}\".format(len(not_in_a) + 1)]:\n",
    "        for c in cell:\n",
    "            c.fill = openpyxl.styles.PatternFill(start_color=\"FFCCCB\", end_color=\"FFCCCB\", fill_type = \"solid\")\n",
    "\n",
    "print(\"处理完成！\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**7、排错与修复**\n",
    "\n",
    "根据提示词生成的源码，拷贝进来运行即可，然后根据运行报错日志逐个根据提示词解决就好，比如本次代码运行就遇到如下的错误：\n",
    "\n",
    "1）复制提示AI提供指引\n",
    "\n",
    "<img src=\"./code_err_panda.png\" style=\"margin-left: 0px\" width=\"800px\">\n",
    "\n",
    "2）安装依赖包：注意切换环境\n",
    "\n",
    "pip install pandas\n",
    "\n",
    "<img src=\"./panda_install.png\" style=\"margin-left: 0px\" width=\"800px\">\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": [],
   "toc_visible": true
  },
  "kernelspec": {
   "display_name": "mkz_py39env",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
